package com.cema.manage.project.manage.rewardRecord.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.cema.manage.framework.web.controller.BaseController;
import com.cema.manage.framework.web.page.TableDataInfo;
import com.cema.manage.project.enums.MoneyTypeEnum;
import com.cema.manage.project.manage.rewardRecord.domain.RewardRecord;
import com.cema.manage.project.manage.rewardRecord.domain.RewardRecordVo;
import com.cema.manage.project.manage.rewardRecord.service.IRewardRecordService;
import com.cema.manage.project.util.DateUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 奖励记录 信息操作处理
 *
 * @author reasahi
 * @date 2024-07-08
 */
@Controller
@RequestMapping("/manage/rewardRecord")
public class RewardRecordController extends BaseController {
    private String prefix = "manage/rewardRecord";

    @Resource
    private IRewardRecordService rewardRecordService;

    @GetMapping()
    @RequiresPermissions("manage:rewardRecord:view")
    public String rewardRecord() {
        return prefix + "/rewardRecord";
    }

    /**
     * 查询奖励记录列表
     */
    @RequiresPermissions("manage:rewardRecord:list")
    @ResponseBody
    @RequestMapping("/list")
    public TableDataInfo list(RewardRecordVo rewardRecordVo) {
        startPage();
        EntityWrapper<RewardRecord> entityWrapper = getEntityWrapper(rewardRecordVo);
        List<RewardRecord> list = rewardRecordService.selectList(entityWrapper);
        return getDataTable(list);
    }

    /**
     * 查询奖励记录列表
     */
    @RequiresPermissions("manage:rewardRecord:list")
    @ResponseBody
    @RequestMapping("/statistics")
    public JSONObject statistics(RewardRecordVo rewardRecordVo) {
        EntityWrapper<RewardRecord> entityWrapper = getEntityWrapper(rewardRecordVo);
        List<RewardRecord> list = rewardRecordService.selectList(entityWrapper);
        Map<String, String> map = new HashMap<>(1);
        long userGoldCoinSum = 0;
        long userRedPacketSum = 0;
        long moneyCommissionGoldCoins = 0;
        long moneyCommissionRedEnvelope = 0;
        long moneyExtensionRedEnvelope = 0;
        if (ObjectUtil.isNull(list)) {
            map.put("userGoldCoin", NumberUtil.decimalFormat(",###", userGoldCoinSum));
            map.put("userRedPacket", NumberUtil.decimalFormat(",###", userRedPacketSum));
            map.put("moneyCommissionGoldCoins", NumberUtil.decimalFormat(",###", moneyCommissionGoldCoins));
            map.put("moneyCommissionRedEnvelope", NumberUtil.decimalFormat(",###", moneyCommissionRedEnvelope));
            map.put("moneyExtensionRedEnvelope", NumberUtil.decimalFormat(",###", moneyExtensionRedEnvelope));
            return new JSONObject(map);
        }
        //过滤集合
        List<RewardRecord> goldCoinList = list.stream().filter(rewardRecord -> rewardRecord.getRrType().equals(MoneyTypeEnum.MONEY_GOLD_COINS.getValue())).
                collect(Collectors.toList());
        if (ObjectUtil.isNotNull(goldCoinList)) {
            userGoldCoinSum = goldCoinList.stream().mapToInt(RewardRecord::getRrMoney).sum();
        }
        List<RewardRecord> redPacketList = list.stream().filter(rewardRecord -> rewardRecord.getRrType().equals(MoneyTypeEnum.MONEY_RED_ENVELOPE.getValue())).
                collect(Collectors.toList());
        if (ObjectUtil.isNotNull(redPacketList)) {
            userRedPacketSum = redPacketList.stream().mapToInt(RewardRecord::getRrMoney).sum();
        }
        List<RewardRecord> moneyCommissionGoldCoinsList = list.stream().filter(rewardRecord -> rewardRecord.getRrType().equals(MoneyTypeEnum.MONEY_COMMISSION_GOLD_COINS.getValue())).
                collect(Collectors.toList());
        if (ObjectUtil.isNotNull(moneyCommissionGoldCoinsList)) {
            moneyCommissionGoldCoins = moneyCommissionGoldCoinsList.stream().mapToInt(RewardRecord::getRrMoney).sum();
        }
        List<RewardRecord> moneyCommissionRedEnvelopeList = list.stream().filter(rewardRecord -> rewardRecord.getRrType().equals(MoneyTypeEnum.MONEY_COMMISSION_RED_ENVELOPE.getValue())).
                collect(Collectors.toList());
        if (ObjectUtil.isNotNull(moneyCommissionRedEnvelopeList)) {
            moneyCommissionRedEnvelope = moneyCommissionRedEnvelopeList.stream().mapToInt(RewardRecord::getRrMoney).sum();
        }
        List<RewardRecord> moneyExtensionRedEnvelopeList = list.stream().filter(rewardRecord -> rewardRecord.getRrType().equals(MoneyTypeEnum.MONEY_EXTENSION_RED_ENVELOPE.getValue())).
                collect(Collectors.toList());
        if (ObjectUtil.isNotNull(moneyExtensionRedEnvelopeList)) {
            moneyExtensionRedEnvelope = moneyExtensionRedEnvelopeList.stream().mapToInt(RewardRecord::getRrMoney).sum();
        }


        map.put("userGoldCoin", NumberUtil.decimalFormat(",###", userGoldCoinSum));
        map.put("userRedPacket", NumberUtil.decimalFormat(",###", userRedPacketSum));
        map.put("moneyCommissionGoldCoins", NumberUtil.decimalFormat(",###", moneyCommissionGoldCoins));
        map.put("moneyCommissionRedEnvelope", NumberUtil.decimalFormat(",###", moneyCommissionRedEnvelope));
        map.put("moneyExtensionRedEnvelope", NumberUtil.decimalFormat(",###", moneyExtensionRedEnvelope));
        return new JSONObject(map);
    }

    @NotNull
    private EntityWrapper<RewardRecord> getEntityWrapper(RewardRecordVo rewardRecordVo) {
        EntityWrapper<RewardRecord> entityWrapper = new EntityWrapper<>();
        //验证是否是代理
        String pusId = rewardRecordVo.getPusId();
        if (ObjectUtil.isNotNull(pusId)) {
            entityWrapper.eq("pus_id", pusId);
        }
        String rrSourceType = rewardRecordVo.getRrSourceType();
        if (StrUtil.isNotEmpty(rrSourceType)) {
            entityWrapper.eq("rr_source_type", rrSourceType);
        }
        String rrType = rewardRecordVo.getRrType();
        if (StrUtil.isNotEmpty(rrType)) {
            entityWrapper.eq("rr_type", rrType);
        }
        Integer plUserId = getUser().getPlUserId();
        if (ObjectUtil.isNotNull(plUserId)) {
            entityWrapper.eq("us_id", plUserId);
        } else {
            String usId = rewardRecordVo.getUsId();
            if (StrUtil.isNotEmpty(usId)) {
                entityWrapper.eq("us_id", usId);
            }
        }
        String usNickname = rewardRecordVo.getUsNickname();
        if (StrUtil.isNotEmpty(usNickname)) {
            entityWrapper.like("us_nickname", usNickname, SqlLike.RIGHT);
        }
        Date startTime = rewardRecordVo.getStartTime();
        if (ObjectUtil.isNotNull(startTime)) {
            entityWrapper.between("create_time", startTime, rewardRecordVo.getEndTime());
        }
        entityWrapper.orderBy("create_time", false);
        return entityWrapper;
    }
}
